package oyy.servlet;

import net.sf.json.JSONObject;
import oyy.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/testuid")
public class DemoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String uid = req.getParameter("uid");
        int i = Integer.parseInt(uid);
        User user = null;
        switch(i){
            case 1: user = new User(1,"冰墩墩","北京");break;
            case 2: user = new User(1,"东方明珠","上海");break;
            case 3: user = new User(1,"羊城","广州");break;
            default: user = new User();
        }
        JSONObject jsonObject = JSONObject.fromObject(user);// 将Java对象转化为json对象
        resp.setContentType("text/html;charset=utf-8");// 设置响应内容的类型
        PrintWriter writer = resp.getWriter();// 获得字符流
        writer.print(jsonObject);// 返回json对象类型的数据
        //System.out.println(jsonObject);
    }
}
